﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;
using Dynamsoft.DotNet.TWAIN;
using Dynamsoft.DotNet.TWAIN.Enums;
namespace KALE_Scanning {
    public partial class Options_FORM : Form {
      

        private bool ifShowPrintUI = false;
        private short JpeqQuality = 70;
        private Dynamsoft.DotNet.TWAIN.Enums.DWTPDFCompressionType PDFCompressionType = Dynamsoft.DotNet.TWAIN.Enums.DWTPDFCompressionType.enumJPEGEncode;
        private DWTPDFPageSize PDFPageSize = DWTPDFPageSize.A4;
        private DWTTIFFCompressionType TIFFCompressionType = DWTTIFFCompressionType.TIFF_RLE;
        private float Resolution = 300;
        private bool DublexScan = true;
        private TWICapPixelType CapPixelType = TWICapPixelType.TWPT_BW;
        private bool UseADF = true;
        private TWCapability Capability = TWCapability.ICAP_AUTODISCARDBLANKPAGES;
        private string PDFProducer = "AGC Yazılım";
        private string PDFAuthor = "AGC Yazılım";
        private string PDFCreator = "AGC Yazılım";
        public Options_FORM() {
            InitializeComponent();
        
            //DisplaySettings();
        }

        private void RestoreSettings() {
            try {
                #region old
                //SPROOTSITEURL = "http://srvport";
                //SCANDPI_X = 600;
                //SCANDPI_Y = 600;
                //SCANWIDTH = 1170;
                //SCANHEIGHT = 1700;
                //SCANCOLORPROFILE = 0;
                //SCANBITDEPTH = 24;
                //DEBUGMODE = true;
                //DEBUGMODE = false; 
                #endregion
                ifShowPrintUI = false;
                JpeqQuality = 70;
                PDFCompressionType = DWTPDFCompressionType.enumJPEGEncode;
                PDFPageSize = DWTPDFPageSize.A4;
                TIFFCompressionType = DWTTIFFCompressionType.TIFF_RLE;
                Resolution = 300;
                DublexScan = true;
                CapPixelType = TWICapPixelType.TWPT_BW;
                UseADF = true;
                Capability = TWCapability.ICAP_AUTODISCARDBLANKPAGES;
                PDFProducer = "AGC Yazılım";
                PDFAuthor = PDFProducer;
                PDFCreator = PDFAuthor;

                Properties.Settings.Default.JPpegQuality = JpeqQuality.ToString();
                Properties.Settings.Default.PDFAuthor = PDFAuthor;
                Properties.Settings.Default.PDFCreator = PDFCreator;
                Properties.Settings.Default.PDFProducer = PDFProducer;

                Properties.Settings.Default.CapPixelType = CapPixelType.ToString();
                Properties.Settings.Default.PDFCompressionType = PDFCompressionType.ToString();
                Properties.Settings.Default.PDFPageSize = PDFPageSize.ToString();
                Properties.Settings.Default.Resolution = Resolution.ToString();
                Properties.Settings.Default.TIFFCompressionType = TIFFCompressionType.ToString();
                Properties.Settings.Default.IfShowPrintUI = ifShowPrintUI.ToString();
                Properties.Settings.Default.UseADF = UseADF.ToString();
                Properties.Settings.Default.IfDuplexScan = DublexScan.ToString();
                Properties.Settings.Default.Capability = Capability.ToString();
                //DisplaySettings();
                LoadSettings();

                MessageBox.Show("Geri yükleme işlemi başarılı...", "Geri Yükleme", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } catch (Exception ex) {
                MessageBox.Show("Geri yükleme sırasında hata oluştu !\n\nError Message: " + ex.Message, "Geri Yükleme", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            SaveSettings();
        }

        private void LoadSettings() {

            txtJpegEncode.Text = Properties.Settings.Default.JPpegQuality;
            txtPDFAuthor.Text = Properties.Settings.Default.PDFAuthor;
            txtPDFCreator.Text = Properties.Settings.Default.PDFCreator;
            txtPDFProducer.Text = Properties.Settings.Default.PDFProducer;
            string pixtype = Properties.Settings.Default.CapPixelType;
            string comp = Properties.Settings.Default.PDFCompressionType;
            string pdfSize = Properties.Settings.Default.PDFPageSize;
            string res = Properties.Settings.Default.Resolution;
            string tiff = Properties.Settings.Default.TIFFCompressionType;
            cmbColorDept.SelectedItem = Properties.Settings.Default.CapPixelType;
            cmbPDFCompressionType.SelectedItem = Properties.Settings.Default.PDFCompressionType;
            cmbPDFPageSize.SelectedItem = Properties.Settings.Default.PDFPageSize;
            cmbResolution.SelectedItem = Properties.Settings.Default.Resolution;
            cmbTiffCompressionType.SelectedItem = Properties.Settings.Default.TIFFCompressionType;
            chkDuplexScan.Checked = Convert.ToBoolean(Properties.Settings.Default.IfDuplexScan);
            chkIfShowUi.Checked = Convert.ToBoolean(Properties.Settings.Default.IfShowPrintUI);
            chkUseAdf.Checked = Convert.ToBoolean(Properties.Settings.Default.UseADF);
            #region old
            //Properties.Settings.Default.SCANDPI_X = Convert.ToInt32(600);
            //Properties.Settings.Default.SCANDPI_Y = Convert.ToInt32(600);
            //Properties.Settings.Default.SCANWIDTH = Convert.ToInt32(1170);
            //Properties.Settings.Default.SCANHEIGHT = Convert.ToInt32(1170);
            //Properties.Settings.Default.SCANCOLORPROFILE = 0;
            //Properties.Settings.Default.SCANBITDEPTH = Convert.ToInt32(24);
            //Properties.Settings.Default.SCANCOMBINEMOD_PDF = radioScanCombine_PDFOn.Checked;

            //Properties.Settings.Default.Save();

            //SPROOTSITEURL = Properties.Settings.Default.SPROOTSITEURL;
            //SCANDPI_X = Properties.Settings.Default.SCANDPI_X;
            //SCANDPI_Y = Properties.Settings.Default.SCANDPI_Y;
            //SCANWIDTH = Properties.Settings.Default.SCANWIDTH;
            //SCANHEIGHT = Properties.Settings.Default.SCANHEIGHT;
            //SCANCOLORPROFILE = Properties.Settings.Default.SCANCOLORPROFILE;
            //SCANBITDEPTH = Properties.Settings.Default.SCANBITDEPTH;
            //SCANCOMBINEMOD_PDF = Properties.Settings.Default.SCANCOMBINEMOD_PDF;
            //DEBUGMODE = Properties.Settings.Default.DEBUGMODE; 
            #endregion
        }

        private void SaveSettings() {
            try {
                
                Properties.Settings.Default.JPpegQuality = txtJpegEncode.Text;
                Properties.Settings.Default.PDFAuthor = txtPDFAuthor.Text;
                Properties.Settings.Default.PDFCreator = txtPDFCreator.Text;
                Properties.Settings.Default.PDFProducer = txtPDFProducer.Text;

                Properties.Settings.Default.Resolution = cmbResolution.SelectedItem.ToString();
                Properties.Settings.Default.CapPixelType = cmbColorDept.SelectedItem.ToString();
                Properties.Settings.Default.PDFCompressionType = cmbPDFCompressionType.SelectedItem.ToString();
                Properties.Settings.Default.PDFPageSize = cmbPDFPageSize.SelectedItem.ToString();
                Properties.Settings.Default.TIFFCompressionType = cmbTiffCompressionType.SelectedItem.ToString();
                Properties.Settings.Default.IfShowPrintUI = chkIfShowUi.Checked.ToString();
                Properties.Settings.Default.IfDuplexScan = chkDuplexScan.Checked.ToString();
                Properties.Settings.Default.UseADF = chkUseAdf.Checked.ToString();

                #region old
                //Properties.Settings.Default.SCANDPI_X = Convert.ToInt32(numUDDPI_X.Value);
                //Properties.Settings.Default.SCANDPI_Y = Convert.ToInt32(numUDDPI_Y.Value);
                //Properties.Settings.Default.SCANWIDTH = Convert.ToInt32(numUDWidth.Value);
                //Properties.Settings.Default.SCANHEIGHT = Convert.ToInt32(numUDHeight.Value);
                //Properties.Settings.Default.SCANCOLORPROFILE = cbColorProfile.SelectedIndex;
                //Properties.Settings.Default.SCANBITDEPTH = Convert.ToInt32(numUDBitDepth.Value);
                //Properties.Settings.Default.SCANCOMBINEMOD_PDF = radioScanCombine_PDFOn.Checked; 
                #endregion

                //Properties.Settings.Default.Upgrade();
                Properties.Settings.Default.Save();
                Properties.Settings.Default.Reload();
                MessageBox.Show("Tarayıcı ayarları güncellendi...", "Tarayıcı Ayarları", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } catch (Exception ex) {
                MessageBox.Show("Tarayıcı ayarları güncellenirken bir hata oluştu!\n\nError Message: " + ex.Message, "Tarayıcı Ayarları", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            LoadSettings();
            //DisplaySettings();
        }

        private void DisplaySettings() {
            
           
        }

        private void buttonHeaderSend_Click(object sender, EventArgs e) {
            SaveSettings();
        }

        private void buttonBottomSend_Click(object sender, EventArgs e) {
            SaveSettings();
        }

        private void buttonHeaderRestore_Click(object sender, EventArgs e) {
            RestoreSettings();
        }

        private void buttonBottomRestore_Click(object sender, EventArgs e) {
            RestoreSettings();
        }

        private void buttonHeaderClose_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void buttonBottomClose_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void cbColorProfile_SelectedIndexChanged(object sender, EventArgs e) {
            //if (cbColorProfile.SelectedIndex == 0) {
            //    SCANBITDEPTH = 24;
            //} else if (cbColorProfile.SelectedIndex == 1) {
            //    SCANBITDEPTH = 8;
            //}
            //numUDBitDepth.Value = SCANBITDEPTH;
        }

        private void labelHeaderTitle_Click(object sender, EventArgs e) {

        }

        private void Options_FORM_Load(object sender, EventArgs e)
        {
            PrepareComboboxes();
            LoadSettings();
        }

        private void PrepareComboboxes()
        {
            cmbResolution.Items.Add("100");
            cmbResolution.Items.Add("200");
            cmbResolution.Items.Add("300");
            cmbResolution.Items.Add("600");

            cmbColorDept.Items.Add(TWICapPixelType.TWPT_BW.ToString());
            cmbColorDept.Items.Add(TWICapPixelType.TWPT_GRAY.ToString());
            cmbColorDept.Items.Add(TWICapPixelType.TWPT_RGB.ToString());

            cmbPDFCompressionType.Items.Add(DWTPDFCompressionType.enumJPEGEncode.ToString());
            cmbPDFCompressionType.Items.Add(DWTPDFCompressionType.enumFlateEncode.ToString());
            cmbPDFCompressionType.Items.Add(DWTPDFCompressionType.enumCCITTFax4Encode.ToString());
            cmbPDFCompressionType.Items.Add(DWTPDFCompressionType.enumAutoEncode.ToString());

            cmbPDFPageSize.Items.Add(DWTPDFPageSize.A3.ToString());
            cmbPDFPageSize.Items.Add(DWTPDFPageSize.A4.ToString());
            cmbPDFPageSize.Items.Add(DWTPDFPageSize.A5.ToString());

            cmbTiffCompressionType.Items.Add(DWTTIFFCompressionType.TIFF_AUTO.ToString());
            cmbTiffCompressionType.Items.Add(DWTTIFFCompressionType.TIFF_NONE.ToString());
            cmbTiffCompressionType.Items.Add(DWTTIFFCompressionType.TIFF_RLE.ToString());



        }
    }
}
